{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 综合实验二\n",
    "\n",
    "欢迎来到列表综合练习！本练习旨在帮助你巩固对 Python 列表的理解和应用能力。练习内容涵盖了列表的创建、访问、修改、排序、遍历、切片等核心知识点。\n",
    "\n",
    "请在每个标记为`# YOUR CODE HERE`的地方填写你的代码，并运行代码单元格来检验结果。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第一部分：列表基础操作\n",
    "\n",
    "这部分练习将重温列表的创建、访问、修改、添加和删除元素等基本操作。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习 1-1：创建和访问列表\n",
    "\n",
    "1. 创建一个名为 `cities` 的列表，包含你最喜欢的五个城市（字符串）。\n",
    "2. 打印整个列表。\n",
    "3. 打印列表中的第一个城市。\n",
    "4. 使用负数索引打印列表中的最后一个城市。\n",
    "5. 使用 f-string 创建一条消息，如 \"我最想去的城市是：[第三个城市名].\" 并打印出来。记得使用 `.title()` 方法让城市名首字母大写。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 创建列表\n",
    "cities = [] # YOUR CODE HERE\n",
    "\n",
    "# 2. 打印整个列表\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 3. 打印第一个城市\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 4. 打印最后一个城市\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 5. 使用 f-string 打印消息\n",
    "# YOUR CODE HERE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习 1-2：修改、添加和删除元素\n",
    "\n",
    "你正在管理一个派对的宾客名单 `guests`。\n",
    "\n",
    "1.  **修改**：名单上的第二位宾客临时换人了，请将其名字修改为 'Carol'。\n",
    "2.  **末尾添加**：你又邀请了一位新朋友 'David'，请将他添加到名单的末尾。\n",
    "3.  **指定位置添加**：你想让 'Frank' 成为名单上的第一位，请将他插入到列表的开头。\n",
    "4.  **按位置删除 (del)**：名单上的第三位宾客有事不能来了，请使用 `del` 语句将他从名单中删除。\n",
    "5.  **弹出元素 (pop)**：最后一位加入的宾客被证明是一位重要的VIP，你需要将他的名字从 `guests` 列表中“弹出”，并存到一个新的变量 `vip_guest` 中，然后打印一条消息欢迎这位VIP。\n",
    "6.  **按值删除 (remove)**：'Alice' 因为行程冲突无法参加，请直接根据她的名字将她从列表中移除。\n",
    "7.  最后，打印出最终的宾客名单，看看还剩下谁。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "guests = ['Alice', 'Bob', 'Eve']\n",
    "print(f\"初始名单: {guests}\")\n",
    "\n",
    "# 1. 修改第二个宾客为 'Carol'\n",
    "# YOUR CODE HERE\n",
    "print(f\"修改后: {guests}\")\n",
    "\n",
    "# 2. 在末尾添加 'David'\n",
    "# YOUR CODE HERE\n",
    "print(f\"添加 David 后: {guests}\")\n",
    "\n",
    "# 3. 在开头插入 'Frank'\n",
    "# YOUR CODE HERE\n",
    "print(f\"插入 Frank 后: {guests}\")\n",
    "\n",
    "# 4. 删除第三位宾客\n",
    "# YOUR CODE HERE\n",
    "print(f\"删除第3位后: {guests}\")\n",
    "\n",
    "# 5. 弹出最后一位宾客并欢迎他\n",
    "# YOUR CODE HERE\n",
    "print(f\"欢迎我们的 VIP, {vip_guest}!\")\n",
    "print(f\"弹出 VIP 后: {guests}\")\n",
    "\n",
    "# 6. 按名字移除 'Alice'\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 7. 打印最终名单\n",
    "print(f\"\\n最终宾客名单: {guests}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二部分：管理和组织列表\n",
    "\n",
    "这部分练习将重点考察列表的排序、反转和长度计算。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习 2-1：排序和反转\n",
    "\n",
    "你有一个记录旅行地点的列表 `places`。\n",
    "\n",
    "1.  **临时排序**：使用 `sorted()` 函数按字母顺序打印列表，并证明原列表的顺序未受影响。\n",
    "2.  **临时反向排序**：使用 `sorted()` 函数按与字母顺序相反的顺序打印列表，并再次证明原列表顺序不变。\n",
    "3.  **永久反转**：使用 `reverse()` 方法反转列表的原始顺序，并打印列表以确认更改。\n",
    "4.  **恢复顺序**：再次调用 `reverse()` 方法，将列表恢复到最初的顺序，并打印确认。\n",
    "5.  **永久排序**：使用 `sort()` 方法按字母顺序永久修改列表，并打印确认。\n",
    "6.  **永久反向排序**：使用 `sort()` 方法按与字母顺序相反的顺序永久修改列表，并打印确认。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "places = ['paris', 'tokyo', 'new york', 'sydney', 'cairo']\n",
    "print(f\"原始列表: {places}\")\n",
    "\n",
    "# 1. 临时排序\n",
    "print(\"\\n临时排序后的列表:\")\n",
    "# YOUR CODE HERE\n",
    "print(\"再次打印原始列表:\")\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 2. 临时反向排序\n",
    "print(\"\\n临时反向排序后的列表:\")\n",
    "# YOUR CODE HERE\n",
    "print(\"再次打印原始列表:\")\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 3. 永久反转\n",
    "print(\"\\n永久反转列表:\")\n",
    "# YOUR CODE HERE\n",
    "print(places)\n",
    "\n",
    "# 4. 恢复顺序\n",
    "print(\"\\n恢复原始顺序:\")\n",
    "# YOUR CODE HERE\n",
    "print(places)\n",
    "\n",
    "# 5. 永久排序\n",
    "print(\"\\n永久按字母排序:\")\n",
    "# YOUR CODE HERE\n",
    "print(places)\n",
    "\n",
    "# 6. 永久反向排序\n",
    "print(\"\\n永久按字母反向排序:\")\n",
    "# YOUR CODE HERE\n",
    "print(places)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习 2-2：列表长度和索引错误\n",
    "\n",
    "1.  对于上一题最终的 `places` 列表，使用 `len()` 函数打印一条消息，说明你访问了多少个地方。\n",
    "2.  下面的代码存在一个 `IndexError`，因为它试图访问一个不存在的索引。请你修复它，让它能正确打印出列表中的最后一个元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 打印列表长度\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 2. 修复索引错误\n",
    "motorcycles = ['honda', 'yamaha', 'suzuki']\n",
    "# 下一行代码会报错，请注释掉错误代码并写出正确的代码\n",
    "# print(motorcycles[3])\n",
    "\n",
    "# YOUR CODE HERE (使用正确的索引打印最后一个元素)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第三部分：列表高级操作\n",
    "\n",
    "这部分练习将考察 `for` 循环、处理数字列表、列表推导式、切片和复制等进阶技能。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习 3-1：遍历列表\n",
    "\n",
    "创建一个你最喜欢的水果列表 `favorite_fruits`。\n",
    "\n",
    "1.  使用 `for` 循环遍历该列表。\n",
    "2.  在循环中，为每种水果打印一条类似 \"我喜欢吃 [水果名]！\" 的消息。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "favorite_fruits = ['apple', 'banana', 'strawberry', 'mango']\n",
    "\n",
    "# 使用 for 循环打印消息\n",
    "# YOUR CODE HERE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习 3-2：创建和使用数字列表\n",
    "\n",
    "1.  使用 `range()` 函数创建一个从 1 到 20（含）的数字列表 `numbers`。\n",
    "2.  打印这个列表。\n",
    "3.  使用 `min()`, `max()`, 和 `sum()` 函数来确认列表的最小值、最大值和总和，并打印出来。\n",
    "4.  使用 `range()` 函数的第三个参数创建一个包含 1 到 20 之间所有奇数的列表 `odd_numbers`，并打印它。\n",
    "5.  **列表推导式**：创建一个列表 `cubes`，包含前 10 个整数（1-10）的立方（即三次方），请使用列表推导式来完成。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1&2. 创建并打印 1-20 的数字列表\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 3. 打印最小值、最大值和总和\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 4. 创建并打印 1-20 的奇数列表\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 5. 使用列表推导式创建前10个整数的立方列表\n",
    "# YOUR CODE HERE\n",
    "print(f\"前10个整数的立方: {cubes}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习 3-3：切片和复制\n",
    "\n",
    "你有一个玩家列表 `players`。\n",
    "\n",
    "1.  **切片**：创建一个切片，包含列表中的前三位玩家，并打印 \"队伍中的前三名玩家是: [前三名玩家]\"。\n",
    "2.  **切片**：创建一个切片，包含列表中间的两位玩家，并打印出来。\n",
    "3.  **切片**：创建一个切片，包含列表中的最后三位玩家，并打印出来。\n",
    "4.  **复制**：复制 `players` 列表到一个名为 `friend_players` 的新列表中。\n",
    "5.  在原始 `players` 列表末尾添加一名新玩家 'Gina'。\n",
    "6.  在 `friend_players` 列表末尾添加一名新玩家 'Henry'。\n",
    "7.  分别打印两个列表，以证明它们是相互独立的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "players = ['charles', 'martina', 'michael', 'florence', 'eli']\n",
    "\n",
    "# 1. 打印前三名玩家\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 2. 打印中间两名玩家\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 3. 打印后三名玩家\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 4. 复制列表\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 5. 修改原列表\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 6. 修改复制的列表\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 7. 打印两个列表以证明独立性\n",
    "print(\"\\n原始玩家列表:\", players)\n",
    "print(\"朋友的玩家列表:\", friend_players)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四部分：综合挑战\n",
    "\n",
    "现在，让我们把所有知识点结合起来，完成一个综合性更强的任务。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 挑战：管理你的披萨订单\n",
    "\n",
    "你正在编写一个披萨店的点餐程序。\n",
    "\n",
    "1.  创建一个披萨配料列表 `toppings`，至少包含 5 种配料。\n",
    "2.  使用 `for` 循环遍历 `toppings` 列表，为顾客打印出所有可用的配料。\n",
    "3.  创建一个空的顾客订单列表 `customer_order`。\n",
    "4.  假设顾客选择了前三种配料，使用切片将这三种配料复制到 `customer_order` 列表中。\n",
    "5.  顾客后来又加了两种配料，请使用 `append()` 方法将它们添加到 `customer_order` 中。\n",
    "6.  打印出顾客最终的订单，并使用 `len()` 告诉他订单里有多少种配料。\n",
    "7.  披萨店的某一种配料（例如，列表中的第四种）刚好用完了，请使用 `remove()` 或 `del` 将它从原始的 `toppings` 列表中移除。\n",
    "8.  最后，按字母顺序打印出更新后的可用配料列表（`toppings`）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 创建配料列表\n",
    "toppings = [] # YOUR CODE HERE\n",
    "\n",
    "# 2. 打印所有可用配料\n",
    "print(\"欢迎光临！我们提供以下配料:\")\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 3. 创建空订单\n",
    "customer_order = []\n",
    "\n",
    "# 4. 顾客选择前三种配料\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 5. 顾客添加新配料\n",
    "toppings_to_add = ['mushrooms', 'extra cheese']\n",
    "# YOUR CODE HERE (使用循环或逐个添加)\n",
    "\n",
    "# 6. 打印最终订单和数量\n",
    "print(\"\\n您的最终披萨订单包含:\", customer_order)\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 7. 移除一种用完的配料 (假设移除 'olives')\n",
    "# YOUR CODE HERE\n",
    "\n",
    "# 8. 打印更新后的可用配料列表\n",
    "# YOUR CODE HERE (先排序再打印)\n",
    "print(\"\\n更新后的可用配料:\", toppings)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
